package pe.edu.ups.studio.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pe.edu.upc.studio.excepcion.DAOExcepcion;
import pe.edu.upc.studio.modelo.Articulo;
import pe.edu.upc.studio.negocio.GestionSalaNegocio;

/**
 * Servlet implementation class InstrumentoServlet
 */
@WebServlet("/InstrumentoServlet")
public class InstrumentoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InstrumentoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String strTipo = request.getParameter("txtTipo");
		String strMarca = request.getParameter("txtMarca");
		String strModelo = request.getParameter("txtModelo");
		String strDes = request.getParameter("txtCaracteristicas");
		Double dblCosto = Double.parseDouble(request.getParameter("txtCosto"));
		
		GestionSalaNegocio objNeg = new GestionSalaNegocio();
		try {
			objNeg.insertarArticulo(strTipo, strMarca, strModelo, strDes, dblCosto, 1);
			Collection<Articulo> lista = new ArrayList<Articulo>();
			lista = objNeg.ListarArticulo(1);
			request.setAttribute("listaarticulos", lista);
			RequestDispatcher rd = request.getRequestDispatcher("registroinstrumento.jsp");
			rd.forward(request, response);
		} catch (DAOExcepcion e) {
			String msj ="No se pudo registrar";
			System.err.println(msj);
			RequestDispatcher rd = request.getRequestDispatcher("/error.jsp?error=" + msj);
			rd.forward(request, response);
		}
	}

}
